package com.wyq.action;

import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.ShowInfo;
import com.wyq.service.ShowInfoService;
import com.wyq.util.PropertiesUtil;
import com.wyq.util.StringUtil;

public class ShowInfoManagerAction extends ActionSupport {
	private ShowInfoService showInfoService;
	private String start;
	private String limit;
	
	private ShowInfo showInfo;
	private String selIDStr;//选中的展会id,逗号分隔的字符串
	
	private String formStr;//
	private File upload;
	private String uploadFileName;
	private String id;//展会ID
	
	public String search() {
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			
			List totalList = showInfoService.getAllShowInfo(null);
			List list = showInfoService.findPageShowInfos(start, limit);
			String jsonStr = "{totalcount:" + totalList.size() + ",showInfoList:" +  JSONArray.fromObject(list).toString() + "}"; 
			out.print(jsonStr);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public String getAllShowInfo() {
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			
			List list = showInfoService.getAllShowInfo(null);
			String jsonStr = "{showInfoList:" +  JSONArray.fromObject(list).toString() + "}"; 
			out.print(jsonStr);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	//新增或更新展会信息
	public String updateOrInsert(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			
			formStr = java.net.URLDecoder.decode(formStr,"UTF-8");
			Map showInfoMap = (Map)JSONUtil.deserialize(formStr);
			showInfoMap.put("id", (id!=null?id:""));
			int flag = -1;
			
			try {
				flag = showInfoService.updateShowInfo(showInfoMap);
				if(flag >= 0){
					//保存成功
					out.print("{success:true,data:'保存成功'}");
				}else{
					out.print("{success:true,data:'主展会已存在，保存存失败'}");
				}

			} catch (Exception e) {
				e.printStackTrace(); 
				out.print("{success:false,reason:'保存失败'}");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public String delete(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			
			try{
				int flag = -100;
				String[] idArr = selIDStr.split(",");
				for(int i=0;i<idArr.length;i++){
					flag = showInfoService.deleteById(idArr[i]);
					
				}
				if(flag > 0){
					out.print("{success:true,data:'操作成功'}");
				}else{
					out.print("{success:true,data:'删除失败，数据不存在'}");
				}
			}catch(Exception e){
				e.printStackTrace();
				out.print("{success:false,reason:'删除失败'}");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	//上传图片
	public String uploadBannerImg(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			//response.setContentType("text/json; charset=utf-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out=response.getWriter();
			String returnStr = "{success:false,reason:'上传失败'}";
			try{
				if (null!= upload && upload.exists() && upload.isFile()) {
					List showInfos = showInfoService.getShowInfoById(id);
					if(null != showInfos && showInfos.size()>0){
						Map showInfoMap = (Map)showInfos.get(0);
						String oldPath = showInfoMap.get("hearderImgPath")!=null?showInfoMap.get("hearderImgPath").toString():"";
						
						String imgSuffix = "";
						if(StringUtil.isNotEmpty(uploadFileName) &&  uploadFileName.indexOf(".") > 0){
							imgSuffix = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
							uploadFileName = System.currentTimeMillis() + "." + imgSuffix;
						}
						String path = ShowInfoManagerAction.class.getClassLoader().getResource("").getPath();  
						String rootPath = path.substring(0, path.indexOf("/WEB-INF/"));  
						rootPath += "/banner/"+uploadFileName;
						File newFile =  new File(rootPath);
						if(!newFile.exists()){
							newFile.createNewFile();
						}
						if(newFile.exists() && newFile.isFile()){
							FileUtils.copyFile(upload, new File(rootPath));
							String filePath = "/banner/"+uploadFileName;
							showInfoMap.put("hearderImgPath", filePath);
							showInfoService.updateShowInfo(showInfoMap);
							
							//删除原图片
							File oldFile = new File(oldPath);
							if(oldFile.exists()){
								oldFile.delete();
							}
							returnStr="{success:true,data:'上传成功'}";
						}
					}
				}else{
					returnStr="{success:true,data:'图片不存在'}";
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			out.print(returnStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public ShowInfoService getShowInfoService() {
		return showInfoService;
	}

	public void setShowInfoService(ShowInfoService showInfoService) {
		this.showInfoService = showInfoService;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getFormStr() {
		return formStr;
	}

	public void setFormStr(String formStr) {
		this.formStr = formStr;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public ShowInfo getShowInfo() {
		return showInfo;
	}

	public void setShowInfo(ShowInfo showInfo) {
		this.showInfo = showInfo;
	}

	public String getSelIDStr() {
		return selIDStr;
	}

	public void setSelIDStr(String selIDStr) {
		this.selIDStr = selIDStr;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}



	
	

}
